//
//  HomeTableCell.m
//  ZZmetro
//
//  Created by 一介布衣 on 2020/6/15.
//  Copyright © 2020 一介布衣. All rights reserved.
//

#import "HomeTableCell.h"

#import "FuncAreaInfo.h"
#import "FuncInfo.h"
#import "HomeItemCell.h"
#import "HomeItemHeader.h"
#import "HomeData.h"
#import "HomeCollectionViewLayout.h"
//#import "HomeHeader.h"

@interface HomeTableCell () <UICollectionViewDataSource, UICollectionViewDelegate, HomeCollectionViewLayoutDelegate>{
    NSMutableArray* funcAreaInfoArr;
}
@property (strong, nonatomic) UICollectionView* collectionView;
@property (nonatomic, assign) CGFloat heightED;

@end

@implementation HomeTableCell


- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        NSLog(@"=================@@@@@@@");
        funcAreaInfoArr = [HomeData getFuncAreaInfos];
        
        //[self setupCollectionView];
        [self addSubview:self.collectionView];
        self.collectionView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.contentView.frame.size.height);
    }
    
    return self;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return funcAreaInfoArr.count;
}

//每个section中的列数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    FuncAreaInfo* funcAreaInfo = [funcAreaInfoArr objectAtIndex:section];
    return funcAreaInfo.funcInfos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    [self updateCollectionViewHeight:self.collectionView.collectionViewLayout.collectionViewContentSize.height];
    
    FuncAreaInfo* funcAreaInfo = [funcAreaInfoArr objectAtIndex:indexPath.section];
    NSMutableArray* funcInfos = funcAreaInfo.funcInfos;
    //if (funcInfos.count <= indexPath.row) {
    //    return cell;
    //}
    
    FuncInfo *info = funcInfos[indexPath.row];
    cell.label.text = info.name;
    cell.imageView.image = [UIImage imageNamed:info.img];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FuncAreaInfo* funcAreaInfo = [funcAreaInfoArr objectAtIndex:indexPath.section];
    NSMutableArray* funcInfos = funcAreaInfo.funcInfos;
    FuncInfo *info = funcInfos[indexPath.row];
    NSLog(@"select event name : %@", info.name);
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItemAtIndexPath:withContent:)]) {
        [self.delegate didSelectItemAtIndexPath:indexPath withContent:info];
    }
}

//header footter

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        HomeItemHeader *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerIdentifier" forIndexPath:indexPath];
        
        [self updateCollectionViewHeight:self.collectionView.collectionViewLayout.collectionViewContentSize.height];
        return reusableview;
    }
    return 0;
}

/*- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return (CGSize){SCREEN_WIDTH, 200};
}*/

- (void)updateCollectionViewHeight:(CGFloat)height{
    if (self.heightED != height) {
        self.heightED = height;
        self.collectionView.frame = CGRectMake(0, 0, self.collectionView.frame.size.width, height);
        
        if (_delegate && [_delegate respondsToSelector:@selector(updateTableViewCellHeight:andheight:andIndexPath:)]) {
            [self.delegate updateTableViewCellHeight:self andheight:height andIndexPath: self.indexPath];
        }
    }
}

#pragma mark ====== init ======
- (UICollectionView *)collectionView {
    if (!_collectionView) {
//        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//        layout.itemSize = CGSizeMake(100, 116);
//        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//        layout.headerReferenceSize = CGSizeMake(self.frame.size.width, 60);
        
        HomeCollectionViewLayout *layout = [[HomeCollectionViewLayout alloc] init];
        layout.delegate = self;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[HomeItemCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeItemHeader class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerIdentifier"];
        
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

#pragma mark -CollectionViewLayoutDelegate

-(CGFloat)heightOfSectionFooterForIndexPath:(NSIndexPath *)indexPath {
    return 0;
}

-(CGFloat)heightOfSectionHeaderForIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

@end
